package com.xk.school_device.exxeption;

import cn.dev33.satoken.exception.NotLoginException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: GlobalExceptionHandler
 * @Author xk
 * @Package com.xk.school_device.exxeption
 * @Date 2025/4/17 10:31
 * @description: 全局异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public Map<String, Object> handleNotLoginException(NotLoginException e) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 401); // 未登录状态码
        result.put("message", "未登录，请先登录");
        // 可以根据不同的未登录类型进行更详细的处理
        switch (e.getType()) {
            case NotLoginException.NOT_TOKEN:
                result.put("detailMsg", "请求未携带 Token");
                break;
            case NotLoginException.INVALID_TOKEN:
                result.put("detailMsg", "Token 无效");
                break;
            case NotLoginException.TOKEN_TIMEOUT:
                result.put("detailMsg", "Token 已过期");
                break;
            case NotLoginException.BE_REPLACED:
                result.put("detailMsg", "Token 已被顶下线");
                break;
            case NotLoginException.KICK_OUT:
                result.put("detailMsg", "Token 已被踢下线");
                break;
            default:
                result.put("detailMsg", "未知未登录原因");
                break;
        }
        return result;
    }
}
